const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');
const time = require('./../../utils/time');

router
    .get('/myInfo', (req, res) => {
        Blog.myInfo.find({}, (err, data) => {
            if (err) {
                res.json({
                    status: 0,
                    message: err
                })
            } else {
                res.json({
                    status: 1,
                    message: '操作成功',
                    data
                })
            }
        });
    })
    // 查看

    .post('/modMyInof', (req, res) => {
        if (req.body.id && req.body.mailbox && req.body.mayun && req.body.introduce) {
            let creationTime = time(new Date().getTime());
            let data = {
                'mailbox': req.body.mailbox,
                'mayun': req.body.mayun,
                'introduce': req.body.introduce,
                'creationTime': creationTime
            }
            if(req.body.portrait) {
                data.portrait = req.body.portrait;
            }
            Blog.myInfo.updateOne({ '_id': req.body.id }, data, err => {
                if (err) {
                    res.json({
                        status: 0,
                        message: '修改失败'
                    })
                } else {
                    res.json({
                        status: 1,
                        message: '修改成功'
                    })
                }
            })
        } else {
            res.json({
                status: 0,
                message: '参数不足'
            })
        }
    })
//修改

module.exports = router;